package com.yantong.service.impl;

import com.yantong.utils.PageUtils;
import com.yantong.mapper.TicketRecordUseMapper;
import com.yantong.service.StatisticsCommanderService;
import com.yantong.pojo.vo.*;
import com.yantong.pojo.ListMemberParams;
import com.yantong.pojo.Member;
import com.yantong.mapper.MemberMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsCommanderServiceImpl implements StatisticsCommanderService{
    
    private static final Logger logger = LoggerFactory.getLogger(StatisticsCommanderServiceImpl.class);
    
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private TicketRecordUseMapper ticketRecordUseMapper;
    
    @Override
    public PageUtils<Member> commanderCommodityStatistics(ListMemberParams listMemberParams, Member member, int offset, int limit){
        logger.info("list commanderCommodityStatistics, listMemberParams:{}, member:{}, offset:{}, limit:{}", listMemberParams, member, offset, limit);
        PageUtils<Member> pageUtils = new PageUtils<>();
        HashMap<String, Object> searchMap = new HashMap<String, Object>();
        if(StringUtils.isNotEmpty(listMemberParams.getStartDate())){
            searchMap.put("startDate", listMemberParams.getStartDate());
        }
        if(StringUtils.isNotEmpty(listMemberParams.getEndDate())){
            searchMap.put("endDate", listMemberParams.getEndDate());
        }
        if(StringUtils.isNotEmpty(member.getProvince())){
            searchMap.put("province", member.getProvince());
        }
        if(StringUtils.isNotEmpty(member.getCity())){
            searchMap.put("city", member.getCity());
        }
        if(StringUtils.isNotEmpty(listMemberParams.getQueryItem())){
            searchMap.put("queryItem", listMemberParams.getQueryItem());
        }
        if(listMemberParams.getCentreId() != null) {
            searchMap.put("centreId", listMemberParams.getCentreId());
        }
        List<Member> memberList = memberMapper.getComCommodityStatistics(searchMap);
        if (memberList != null && memberList.size()>0) {
            logger.info("list commanderCommodityStatistics, total size:{}", memberList.size());
            pageUtils.setTotal(memberList.size());
            Long total = 0L;
            for(Member mem : memberList){
                total += mem.getAlltotal();
            }
            logger.info("CommanderCommodityStatistics 门店商品销售统计 > 总商品数:{}", total);
            List<Member> memList = memberMapper.getComCommodityStatistics(searchMap, new RowBounds(offset, limit));
            for(Member memb : memList){
                if(total>0){
                    String scale = new DecimalFormat("0.00").format((double)memb.getAlltotal()/total*100)+"";
                    if(StringUtils.isNotEmpty(scale)){
                        memb.setScale(scale);
                    }else{
                        memb.setScale("");
                    }
                }else{
                    memb.setScale("");
                }
            }
            pageUtils.setRows(memList);
        }else {
            logger.info("list members not found");
            pageUtils.setRows(new ArrayList<>());
        }
        return pageUtils;
    }
    
    @Override
    public Long getJyTotal(ListMemberParams listMemberParams, Member member){
        logger.info("GetJyTotal, listMemberParams:{}, member:{}, offset:{}, limit:{}", listMemberParams, member);
        HashMap<String, Object> searchMap = new HashMap<String, Object>();
        if(StringUtils.isNotEmpty(listMemberParams.getStartDate())){
            searchMap.put("startDate", listMemberParams.getStartDate());
        }
        if(StringUtils.isNotEmpty(listMemberParams.getEndDate())){
            searchMap.put("endDate", listMemberParams.getEndDate());
        }
        if(StringUtils.isNotEmpty(member.getProvince())){
            searchMap.put("province", member.getProvince());
        }
        if(StringUtils.isNotEmpty(member.getCity())){
            searchMap.put("city", member.getCity());
        }
        if(StringUtils.isNotEmpty(listMemberParams.getQueryItem())){
            searchMap.put("queryItem", listMemberParams.getQueryItem());
        }
        if(listMemberParams.getCentreId() != null) {
            searchMap.put("centreId", listMemberParams.getCentreId());
        }
        List<Member> memberList = memberMapper.getComCommodityStatistics(searchMap);
        if (memberList != null && memberList.size()>0) {
            logger.info("GetJyTotal, total size:{}", memberList.size());
            Long total = 0L;
            for(Member mem : memberList){
                total += mem.getAlltotal();
            }
            logger.info("GetJyTotal 门店商品销售统计 > 商品交易总量:{}", total);
            return total;
        }else {
            logger.info("GetJyTotal is zero");
            return 0L;
        }
    }
    
    @Override
    public List<Member> statisticsExport(ListMemberParams listMemberParams, Member member){
        logger.info("list statisticsExport, listMemberParams:{}, member:{}", listMemberParams, member);
        HashMap<String, Object> searchMap = new HashMap<String, Object>();
        if(StringUtils.isNotEmpty(listMemberParams.getStartDate())){
            searchMap.put("startDate", listMemberParams.getStartDate());
        }
        if(StringUtils.isNotEmpty(listMemberParams.getEndDate())){
            searchMap.put("endDate", listMemberParams.getEndDate());
        }
        if(StringUtils.isNotEmpty(member.getProvince())){
            searchMap.put("province", member.getProvince());
        }
        if(StringUtils.isNotEmpty(member.getCity())){
            searchMap.put("city", member.getCity());
        }
        if(StringUtils.isNotEmpty(listMemberParams.getQueryItem())){
            searchMap.put("queryItem", listMemberParams.getQueryItem());
        }
        if(listMemberParams.getCentreId() != null) {
            searchMap.put("centreId", listMemberParams.getCentreId());
        }
        List<Member> memberList = memberMapper.getComCommodityStatistics(searchMap);
        if (memberList != null && memberList.size()>0) {
            logger.info("list statisticsExport, total size:{}", memberList.size());
            Long total = 0L;
            for(Member mem : memberList){
                total += mem.getAlltotal();
            }
            logger.info("statisticsExport 门店商品销售统计 > 导出 > 总商品数:{}", total);
            List<Member> memList = memberMapper.getComCommodityStatistics(searchMap);
            for(Member memb : memList){
                if(total>0){
                    String scale = new DecimalFormat("0.00").format((double)memb.getAlltotal()/total*100)+"";
                    if(StringUtils.isNotEmpty(scale)){
                        memb.setScale(scale);
                    }else{
                        memb.setScale("");
                    }
                }else{
                    memb.setScale("");
                }
            }
            return memList;
        }else {
            logger.info("list statisticsExport not found");
            return null;
        }
    }

    @Override
    public StoreSalesStatsVo getStoreItemSalesStatsSum(DataStatsDto dataStatsDto) {
        return memberMapper.getStoreItemSalesStatsSum(dataStatsDto);
    }

    @Override
    public List<StoreSalesStatsVo> getStoreItemSalesStats(DataStatsDto dataStatsDto) {
        return memberMapper.getStoreItemSalesStats(dataStatsDto);
    }

    @Override
    public Integer getUserachievementDataCount(DataStatsDto dataStatsDto) {
        return memberMapper.getUserachievementDataCount(dataStatsDto);
    }

    @Override
    public List<UserAchievementVo> getUserachievementData(DataStatsDto dataStatsDto, RowBounds rowBounds) {
        return memberMapper.getUserachievementData(dataStatsDto,rowBounds);
    }

    @Override
    public Map<Long, OrderAchievementVo> getUserSelfStats(Map map) {
        return memberMapper.getUserSelfStats(map);
    }

    @Override
    public Map<Long,OrderAchievementVo> getUserShareStats(Map map) {
        return memberMapper.getUserShareStats(map);
    }

    @Override
    public List<UserAchievementVo> getTeamAchievementStats(DataStatsDto dataStatsDto) {
        return memberMapper.getTeamAchievementStats(dataStatsDto);
    }

    @Override
    public List<UserAchievementVo> getRoleAchievementStats(DataStatsDto dataStatsDto) {
        return memberMapper.getRoleAchievementStats(dataStatsDto);
    }

    @Override
    public Integer getCentreAchievementStatsCount(DataStatsDto dataStatsDto) {
        return memberMapper.getCentreAchievementStatsCount(dataStatsDto);
    }

    @Override
    public List<UserAchievementVo> getCentreAchievementStats(DataStatsDto dataStatsDto, RowBounds rowBounds) {
        return memberMapper.getCentreAchievementStats(dataStatsDto, rowBounds);
    }

    @Override
    public List<StoreVerifyStatsVo> getStoreVerifyStats(Long teamId) {
        return ticketRecordUseMapper.getStoreVerifyStats(teamId);
    }

    @Override
    public List<StoreVerifyStatsVo> getStoreVerifyStatsDetail(Map map) {
        return ticketRecordUseMapper.getStoreVerifyStatsDetail(map);
    }
}
